﻿using System;

using System.Windows.Forms;
using ScannerApp.ScannerWebserviceWrapper;
using CodeBetter.Json;
using ScannerApp.Forms;
using System.Drawing;




namespace ScannerApp
{
    public partial class LoginForm : Form
    {
        SettingsForm settingsForm;
        MainForm mainForm;

        public LoginForm()
        {
            
            InitializeComponent();
            this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            if (AppConfig.Instance.GetSettingValue(AppConfig.RememberMeKey).ToLower().Equals(true.ToString().ToLower()))
            {
                this.txtUserName.Text = AppConfig.Instance.GetSettingValue(AppConfig.UserNameKey);
                this.txtPassword.Text = AppConfig.Instance.GetSettingValue(AppConfig.PasswordKey);
                this.chkRememberMe.Checked = Boolean.Parse(AppConfig.Instance.GetSettingValue(AppConfig.RememberMeKey));
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            OpenSettingsForm();
        }

        private void linkSettings_Click(object sender, EventArgs e)
        {
            OpenSettingsForm();
        }

        void OpenSettingsForm()
        {
            if (settingsForm == null)
            {
                settingsForm = new SettingsForm();
            }

            settingsForm.Show();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtUserName.Text.Trim() == "")
                {
                    MessageBox.Show("用户名不得为空！");
                    return;
                }
                else if (this.txtPassword.Text == "")
                {
                    MessageBox.Show("密码不得为空！");
                    return;
                }
                else
                {
                    ScannerWebServiceProxy.Instance.Url = AppConfig.Instance.GetSettingValue(AppConfig.WebServiceUrlKey);

                    string strResult = ScannerWebServiceProxy.Instance.login(this.txtUserName.Text.Trim(), this.txtPassword.Text);

                    BaseResult<String> result = Converter.Deserialize<BaseResult<String>>(strResult);
                    
                    if (!result.IsSucessful())
                    {
                        MessageBox.Show("账号错误！");
                        return;

                    }
                    else
                    {

                        string userName = "";
                        string pwd = "";
                        string rememberMe = this.chkRememberMe.Checked.ToString().ToLower();
                        if (chkRememberMe.Checked)
                        {
                            userName = this.txtUserName.Text.Trim();
                            pwd = this.txtPassword.Text;
                          
                        }
                        BaseParameter.staticUserName = this.txtUserName.Text.Trim();
                        BaseParameter.staticPassword = this.txtPassword.Text;

                        AppConfig.Instance.SetSettingValue(AppConfig.UserNameKey, userName);
                        AppConfig.Instance.SetSettingValue(AppConfig.PasswordKey, pwd);
                        AppConfig.Instance.SetSettingValue(AppConfig.RememberMeKey, rememberMe);
                        AppConfig.Instance.Save();

                        

                        if (mainForm == null)
                        {
                            mainForm = new MainForm();
                        }


                        mainForm.ShowDialog();

                        this.Close();

                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}